#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_sList)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/sList.h>
#include <varch/tool.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "sList.h"
#include "tool.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

static void test_create(void);
static void test_set(void);
static void test_insert(void);
static void test_erase(void);
static void test_attach(void);
static void test_detach(void);
static void test_push(void);
static void test_pop(void);
static void test_to(void);
static void test_size(void);
static void test_append(void);
static void test_copy(void);
static void test_reverse(void);

static void test_create(void)
{
    sList *list = NULL;

    list = sList_create();
    if (!list)
    {
        printf("sList_create Fail!\r\n");
        return;
    }

    printf("sList_create Success!\r\n");

    sList_delete(list);
}

static void test_set(void)
{
    sList *list = NULL;
    int dataInt = 3;
    char *dataString = "Hello sList";
    char outBuffer[32];

    list = sList_create();
    if (!list)
    {
        printf("sList_create Fail!\r\n");
        return;
    }

    printf("sList_create Success!\r\n");

    sList_set(list, &dataInt, sizeof(dataInt));
    printf("list->data %d\r\n", sList_ref(list, int));

    dataInt = 100;
    sList_set(list, &dataInt, sizeof(dataInt));
    printf("list->data %d\r\n", sList_ref(list, int));

    sList_set(list, dataString, strlen(dataString) + 1);
    printf("list->data %s\r\n", ((char *)(list->data)));

    sList_get(list, outBuffer, sizeof(outBuffer));
    printf("get data %s\r\n", outBuffer);

    sList_delete(list);
}

static void test_insert(void)
{
    sList *list = NULL;

    for (int i = 0; i < 5; i++)
    {
        if (!sList_insert(&list, -1, &i, sizeof(i))) goto FAIL;
    }

    sList_forEach(list, n)
    {
        printf("data %d\r\n", sList_ref(n, int));
    }

FAIL:
    sList_delete(list);
}

static void test_erase(void)
{
    sList *list = NULL;

    for (int i = 0; i < 5; i++)
    {
        if (!sList_insert(&list, -1, &i, sizeof(i))) goto FAIL;
    }

    sList_erase(&list, 0, NULL);

    sList_forEach(list, n)
    {
        printf("data %d\r\n", sList_ref(n, int));
    }

FAIL:
    sList_delete(list);
}

static void test_attach(void)
{
    sList *list = NULL, *a = NULL;

    for (int i = 0; i < 5; i++)
    {
        if (!sList_pushBack(&list, &i, sizeof(i))) goto FAIL;
    }
    for (int i = 0; i < 3; i++)
    {
        if (!sList_pushBack(&a, &i, sizeof(i))) goto FAIL;
    }

    sList_attach(&list, -1, a);

    sList_forEach(list, n)
    {
        printf("data %d\r\n", sList_ref(n, int));
    }

FAIL:
    sList_delete(list);
}

static void test_detach(void)
{
    sList *list = NULL, *node;

    for (int i = 0; i < 10; i++)
    {
        if (!sList_insert(&list, -1, &i, sizeof(i))) goto FAIL;
    }

    node = sList_detach(&list, 3, -1, NULL);
    if (!node)
    {
        printf("sList_detach fail\r\n");
    }

    sList_forEach(node, n)
    {
        printf("node data %d\r\n", sList_ref(n, int));
    }

    sList_delete(node);

    sList_forEach(list, n)
    {
        printf("data %d\r\n", sList_ref(n, int));
    }

FAIL:
    sList_delete(list);
}

static void test_push(void)
{
    sList *list = NULL;

    for (int i = 0; i < 5; i++)
    {
        if (!sList_pushFront(&list, &i, sizeof(i))) goto FAIL;
    }
    for (int i = 0; i < 5; i++)
    {
        if (!sList_pushBack(&list, &i, sizeof(i))) goto FAIL;
    }

    sList_forEach(list, n)
    {
        printf("data %d\r\n", sList_ref(n, int));
    }

FAIL:
    sList_delete(list);
}

static void test_pop(void)
{
    sList *list = NULL;

    for (int i = 0; i < 5; i++)
    {
        if (!sList_pushFront(&list, &i, sizeof(i))) goto FAIL;
    }
    for (int i = 0; i < 5; i++)
    {
        if (!sList_pushBack(&list, &i, sizeof(i))) goto FAIL;
    }

    sList_popBack(&list);
    sList_popBack(&list);
    sList_popFront(&list);

    sList_forEach(list, n)
    {
        printf("data %d\r\n", sList_ref(n, int));
    }

FAIL:
    sList_delete(list);
}

static void test_to(void)
{
    sList *list = NULL, *node;

    for (int i = 0; i < 10; i++)
    {
        if (!sList_pushBack(&list, &i, sizeof(i))) goto FAIL;
    }

    node = sList_to(list, 6);
    if (!node)
    {
        printf("sList_to fail\r\n");
        goto FAIL;
    }

    printf("data %d\r\n", sList_ref(node, int));

FAIL:
    sList_delete(list);
}

static void test_size(void)
{
    sList *list = NULL;

    for (int i = 0; i < 10; i++)
    {
        if (!sList_pushBack(&list, &i, sizeof(i))) goto FAIL;
    }

    printf("size %d\r\n", sList_size(list));
    printf("size %d\r\n", sList_size(sList_to(list, 3)));

FAIL:
    sList_delete(list);
}

static void test_append(void)
{
    sList *list = NULL, *ap = NULL;

    for (int i = 0; i < 10; i++)
    {
        if (!sList_pushBack(&list, &i, sizeof(i))) goto FAIL;
        if (!sList_pushBack(&ap, &i, sizeof(i))) goto FAIL;
    }

    if (!sList_append(list, &ap)) goto FAIL;

    printPoint(ap);

    sList_forEach(list, n)
    {
        printf("data %d\r\n", sList_ref(n, int));
    }

FAIL:
    sList_delete(list);
    sList_delete(ap);
}

static void test_copy(void)
{
    sList *list = NULL, *copy = NULL;

    for (int i = 0; i < 10; i++)
    {
        if (!sList_pushBack(&list, &i, sizeof(i))) goto FAIL;
    }

    copy = sList_copy(list, 2, -1);
    if (!copy)
    {
        printf("sList_copy fail\r\n");
    }

    sList_forEach(copy, n)
    {
        printf("data %d\r\n", sList_ref(n, int));
    }

FAIL:
    sList_delete(list);
    sList_delete(copy);
}

static void test_reverse(void)
{
    sList *list = NULL;

    for (int i = 0; i < 10; i++)
    {
        if (!sList_pushBack(&list, &i, sizeof(i))) goto FAIL;
    }

    if (!sList_reverse(list, sList_front, sList_back))
    {
        printf("sList_reverse fail\r\n");
    }

    sList_forEach(list, n)
    {
        printf("data %d\r\n", sList_ref(n, int));
    }

FAIL:
    sList_delete(list);
}

static void test_base(void)
{
    // test_create();
    test_set();
    // test_insert();
    // test_erase();
    // test_attach();
    // test_detach();
    // test_push();
    // test_pop();
    // test_to();
    // test_size();
    // test_append();
    // test_copy();
    // test_reverse();
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: sList [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        <create>    Test create and delete functions\n"
"                        <set>       Test set function\n"
"                        <insert>    Test insert function\n"
"                        <erase>     Test erase function\n"
"                        <attach>    Test attach function\n"
"                        <detach>    Test detach function\n"
"                        <push>      Test push function\n"
"                        <pop>       Test pop function\n"
"                        <to>        Test locate to specify index function\n"
"                        <size>      Test size function\n"
"                        <append>    Test append function\n"
"                        <copy>      Test copy function\n"
"                        <reverse>   Test reverse function\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"

    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("sList version %d.%d.%d\r\n", SLIST_V_MAJOR, SLIST_V_MINOR, SLIST_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            srand((unsigned int)time(NULL));
            #if defined(TEST_TARGET_sList)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
        else if (!strcmp(execute, "create"))
        {
            test_create();
        }
        else if (!strcmp(execute, "set"))
        {
            test_set();
        }
        else if (!strcmp(execute, "insert"))
        {
            test_insert();
        }
        else if (!strcmp(execute, "erase"))
        {
            test_erase();
        }
        else if (!strcmp(execute, "attach"))
        {
            test_attach();
        }
        else if (!strcmp(execute, "detach"))
        {
            test_detach();
        }
        else if (!strcmp(execute, "push"))
        {
            test_push();
        }
        else if (!strcmp(execute, "pop"))
        {
            test_pop();
        }
        else if (!strcmp(execute, "to"))
        {
            test_to();
        }
        else if (!strcmp(execute, "size"))
        {
            test_size();
        }
        else if (!strcmp(execute, "append"))
        {
            test_append();
        }
        else if (!strcmp(execute, "copy"))
        {
            test_copy();
        }
        else if (!strcmp(execute, "reverse"))
        {
            test_reverse();
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_sList)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_sList(void)
{
    command_export("sList", test);
}
init_export_app(test_sList);
#endif 
